package org.jf.tool.fx.jftoolfx.encrypt.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import org.jf.tool.fx.jftoolfx.common.BaseController;
import org.jf.tool.fx.jftoolfx.util.HmacUtil;

import java.io.File;
import java.net.URL;
import java.util.Base64;
import java.util.HexFormat;
import java.util.ResourceBundle;

/**
 * @author jifeng_zhang
 * @date 2025/8/15 下午3:43
 */
public class HmacController extends BaseController {

    @FXML
    public VBox main;
    @FXML
    public HBox fileBox;
    @FXML
    public TextField keyText;
    @FXML
    public TextField filePathField;
    @FXML
    private TextArea plainText;
    @FXML
    private TextArea resultText;
    @FXML
    private ComboBox algorithmBox;
    @FXML
    public ToggleGroup keyTypeToggleGroup;
    @FXML
    private ToggleGroup myToggleGroup;
    @FXML
    private ToggleGroup resultToggleGroup;

    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {
    }

    @FXML
    private void changeType() {
        String type = myToggleGroup.getSelectedToggle().getUserData().toString();
        if ("1".equals(type)) {
            fileBox.setVisible(false);
            fileBox.setManaged(false);
            plainText.setVisible(true);
            plainText.setManaged(true);
        } else if ("2".equals(type)) {
            plainText.setVisible(false);
            plainText.setManaged(false);
            fileBox.setVisible(true);
            fileBox.setManaged(true);
        }
    }

    @FXML
    private void upload() {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择要上传的文件");

        // 设置文件过滤器（可选）
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("所有文件 (*.*)", "*.*");
        fileChooser.getExtensionFilters().add(filter);
        // 设置初始目录为上次使用的目录
        if (LAST_DIRECTORY != null) {
            fileChooser.setInitialDirectory(LAST_DIRECTORY);
        } else {
            fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
        }

        // 获取当前窗口
        File file = fileChooser.showOpenDialog(TOP_STAGE);
        if (file != null) {
            LAST_DIRECTORY = file.getParentFile();
            filePathField.setText(file.getAbsolutePath());
        } else {
            filePathField.setText("");
        }
    }

    @FXML
    private void encrypt() {
        try {
            String algorithm = algorithmBox.getValue().toString();
            String type = myToggleGroup.getSelectedToggle().getUserData().toString();
            String resultType = resultToggleGroup.getSelectedToggle().getUserData().toString();
            String keyStr = keyText.getText();
            byte[] key;
            String keyType = keyTypeToggleGroup.getSelectedToggle().getUserData().toString();
            if ("base64".equals(keyType)) {
                key = Base64.getDecoder().decode(keyStr);
            } else if ("hex".equals(keyType)) {
                key = HexFormat.of().parseHex(keyStr);
            } else {
                key = keyStr.getBytes();
            }
            String resultStr = "";
            if ("1".equals(type)) {
                String plain = plainText.getText();
                byte[] result = HmacUtil.generateHmac(algorithm, key, plain.getBytes());
                if ("hex".equalsIgnoreCase(resultType)) {
                    resultStr = HexFormat.of().formatHex(result);
                } else {
                    resultStr = Base64.getEncoder().encodeToString(result);
                }
            } else {
                File file = new File(filePathField.getText());
                if (file.exists()) {
                    byte[] result = HmacUtil.generateHmac(algorithm, key, file);
                    if ("hex".equalsIgnoreCase(resultType)) {
                        resultStr = HexFormat.of().formatHex(result);
                    } else {
                        resultStr = Base64.getEncoder().encodeToString(result);
                    }
                } else {
                    resultStr = "请先上传文件！";
                }
            }
            resultText.setText(resultStr);
        } catch (Exception e) {
            resultText.setText(e.getMessage());
        }
    }

    @FXML
    private void clear() {
        plainText.setText("");
        resultText.setText("");
        filePathField.setText("");
    }


    @FXML
    private void copy(ActionEvent event) {
        copy(resultText.getText());
    }

}
